#ifdef __MYSQL__
#include "RtBase.h"
#ifndef WIN32
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#endif

#include "dbmysql.h"

CDBException_::CDBException_()
{
	Empty();
}

CDBException_::~CDBException_()
{
}

void CDBException_::SetErrorString(short nRetCode, LPCSTR lpszError)
{
	m_nRetCode = nRetCode;
	strcpy((char*)m_szError, lpszError);
}

void CDBException_::Empty()
{
	m_nRetCode = 0;
	memset(m_szError, 0, _MAX_ERRMESSAGE);
}

void _DB_ThrowException(short nRetCode, LPCSTR lpszError)
{
	CDBException_* e = new CDBException_();
	e->SetErrorString(nRetCode, lpszError);
	throw(e);
}

CDatabase_::CDatabase_()
{
	m_mysql = NULL;
	m_bConnected = FALSE;
}

CDatabase_::~CDatabase_()
{
	Close();
}

int  CDatabase_::Open( LPBYTE dbserver, LPBYTE database, 
							LPBYTE username, LPBYTE password,
							BOOL  bReadOnly)
{
	CRtString dbserver_tmp = (char*)dbserver;
	CRtString db_tmp = (char*) database;
	CRtString username_tmp = (const char*)username;
	CRtString passwd_tmp = (const char*)password;

	if(m_bConnected)
	{
		if(dbserver_tmp == m_dbserver && db_tmp == m_database && username_tmp == m_username && passwd_tmp == m_password)
			return DB_SUCCESS;
	}
	
	
	m_dbserver = dbserver_tmp;
	m_database = db_tmp;
	m_username = username_tmp;
	m_password = passwd_tmp;
	return Open();

}

int CDatabase_::Open()
{
	Close();
	m_mysql = mysql_init(NULL);
	if(m_mysql == NULL)
	{
		_DB_ThrowException(mysql_errno(m_mysql), mysql_error(m_mysql));
		return DB_FAIL_INITIALIZE_PROCESS_ENVIRONMENT;
	}
	mysql_options(m_mysql,MYSQL_SET_CHARSET_NAME,"utf8");

	if (mysql_real_connect(m_mysql,
							m_dbserver.c_str(),
							m_username.c_str(),
							m_password.c_str(),
							m_database.c_str(),
							0,
							NULL,
							0) == NULL)
	{
		//printf("open database error, reaon==%s\n",mysql_error(m_mysql));
		_DB_ThrowException(mysql_errno(m_mysql), mysql_error(m_mysql));
		return DB_ERROR;
	}
	m_bConnected = TRUE;
	return DB_SUCCESS;
}

int  CDatabase_::Close()
{
	if(m_mysql)
	{
		mysql_close(m_mysql);
		m_mysql = NULL;
	}
	m_bConnected = FALSE;
	return DB_SUCCESS;
}

int  CDatabase_::ExecuteSQLDirect( LPCSTR sql_stmt, 
								CRecordset_ *pResultset,
								BOOL bRemoted)
{
	if(!sql_stmt)
	{
		return DB_ERROR;
	}

	if(!m_bConnected && DB_SUCCESS != Open())
	{
		_DB_ThrowException(DB_ERROR,"still not initialize");
		return DB_ERROR;
	}

	int iLive = mysql_ping(m_mysql) ; //check if the connection is still alive ,if not try reconnecting
	if(0 != iLive && DB_SUCCESS != Open())
	{
		_DB_ThrowException(DB_ERROR,"still not initialize");
		return DB_ERROR;
	}

	if (mysql_query(m_mysql, sql_stmt) != 0)
	{
		_DB_ThrowException(mysql_errno(m_mysql), mysql_error(m_mysql));
		return DB_ERROR;
	}

	if(pResultset)
	{
		pResultset->FillResultSet(bRemoted);
	}
	return DB_SUCCESS;
}

int CDatabase_::InsertAutoIncRow(LPCSTR sql_stmt,
						LONGLONG& autoNumber)
{
	if(!sql_stmt)
	{
		return DB_ERROR;
	}

	if(!m_bConnected && DB_SUCCESS != Open())
	{
		_DB_ThrowException(DB_ERROR,"still not initialize");
		return DB_ERROR;
	}

	int iLive = mysql_ping(m_mysql) ; //check if the connection is still alive ,if not try reconnecting
	if(0 != iLive && DB_SUCCESS != Open())
	{
		_DB_ThrowException(DB_ERROR,"still not initialize");
		return DB_ERROR;
	}

	if (mysql_query(m_mysql, sql_stmt) != 0)
	{
		_DB_ThrowException(mysql_errno(m_mysql), mysql_error(m_mysql));
		return DB_ERROR;
	}
	my_ulonglong ulAuto = mysql_insert_id(m_mysql);
	autoNumber = (LONGLONG)ulAuto;
	return DB_SUCCESS;
}

CRecordset_::CRecordset_(CDatabase_* pDB)
{
	m_pDB = pDB;
	//RT_ASSERTE(m_pDB);
	m_result = NULL;
	//m_row = NULL;
	m_AffectRows = 0;
}

CRecordset_::~CRecordset_()
{
	Destroy();
}

int CRecordset_::Destroy()
{
	if(m_result)
	{
		mysql_free_result(m_result);
		m_result = NULL;
	}
	m_RecordCount = m_ColumnCount = 0;
	return DB_SUCCESS;
}

int CRecordset_::FillResultSet(BOOL bRemote)
{
	if(!bRemote)
		m_result = mysql_store_result(m_pDB->m_mysql);
	else
		m_result = mysql_use_result(m_pDB->m_mysql);
	m_AffectRows = mysql_affected_rows(m_pDB->m_mysql);
	if(!m_result)
	{
		m_RecordCount = m_ColumnCount = 0;
		return DB_SUCCESS;
	}
	m_RecordCount = mysql_num_rows(m_result);
	m_ColumnCount = mysql_num_fields(m_result);
	return DB_SUCCESS;
}

MYSQL_ROW	CRecordset_::GetRecord(DWORD dwIndex)
{
	if(!m_result)
		return NULL;
	mysql_data_seek(m_result, dwIndex);
	return mysql_fetch_row(m_result);
}

#endif

